package com.shuda.business.channel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.channel.entity.ChannelEntity;
import com.shuda.business.channel.service.ChannelService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.result.Result;
import com.shuda.common.utils.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 渠道管理控制器类
 */
@RestController
@RequestMapping("/api/channel")
@Tag(name = "渠道管理", description = "渠道管理相关接口")
@RequiredArgsConstructor
@Validated
public class ChannelController {

    private final ChannelService channelService;

    /**
     * 分页查询渠道列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询渠道列表")
    public Result<PageResult<ChannelEntity>> getChannelPage(@Valid PageDTO pageDTO,
                                                           @RequestParam(required = false) String channelName,
                                                           @RequestParam(required = false) String channelType,
                                                           @RequestParam(required = false) String channelCode,
                                                           @RequestParam(required = false) String status) {
        QueryWrapper<ChannelEntity> queryWrapper = new QueryWrapper<>();
        if (channelName != null && !channelName.trim().isEmpty()) {
            queryWrapper.like("channel_name", channelName);
        }
        if (channelType != null && !channelType.trim().isEmpty()) {
            queryWrapper.eq("channel_type", channelType);
        }
        if (channelCode != null && !channelCode.trim().isEmpty()) {
            queryWrapper.like("channel_code", channelCode);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        
        PageResult<ChannelEntity> pageResult = channelService.getChannelPage(pageDTO, queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取渠道详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取渠道详情")
    public Result<ChannelEntity> getChannelById(@PathVariable Long id) {
        ChannelEntity channel = channelService.getById(id);
        return Result.success(channel);
    }

    /**
     * 创建渠道
     */
    @PostMapping
    @Operation(summary = "创建渠道")
    public Result<String> createChannel(@Valid @RequestBody ChannelEntity channel) {
        channelService.createChannel(channel);
        return Result.success("渠道创建成功");
    }

    /**
     * 更新渠道
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新渠道")
    public Result<String> updateChannel(@PathVariable Long id, @Valid @RequestBody ChannelEntity channel) {
        channel.setId(id);
        channelService.updateChannel(channel);
        return Result.success("渠道更新成功");
    }

    /**
     * 删除渠道
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除渠道")
    public Result<String> deleteChannel(@PathVariable Long id) {
        channelService.deleteChannel(id);
        return Result.success("渠道删除成功");
    }

    /**
     * 批量删除渠道
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除渠道")
    public Result<String> deleteChannels(@RequestBody List<Long> ids) {
        channelService.deleteChannels(ids);
        return Result.success("渠道批量删除成功");
    }

    /**
     * 更新渠道状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新渠道状态")
    public Result<String> updateChannelStatus(@PathVariable Long id, @RequestParam String status) {
        channelService.updateChannelStatus(id, status);
        return Result.success("渠道状态更新成功");
    }

    /**
     * 测试渠道连接
     */
    @PostMapping("/{id}/test")
    @Operation(summary = "测试渠道连接")
    public Result<String> testChannel(@PathVariable Long id) {
        boolean result = channelService.testChannel(id);
        if (result) {
            return Result.success("渠道连接测试成功");
        } else {
            return Result.error("渠道连接测试失败");
        }
    }

    /**
     * 获取渠道配置
     */
    @GetMapping("/{id}/config")
    @Operation(summary = "获取渠道配置")
    public Result<ChannelEntity> getChannelConfig(@PathVariable Long id) {
        ChannelEntity config = channelService.getChannelConfig(id);
        return Result.success(config);
    }

    /**
     * 更新渠道配置
     */
    @PutMapping("/{id}/config")
    @Operation(summary = "更新渠道配置")
    public Result<String> updateChannelConfig(@PathVariable Long id, @RequestBody ChannelEntity config) {
        channelService.updateChannelConfig(id, config);
        return Result.success("渠道配置更新成功");
    }
}